package icexam.exam.print;

/*
 * NewWaitingDialog.java
 *
 * Created on 2007��4��28��, ����5:49
 */
;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.plaf.metal.MetalProgressBarUI;

/**
 *
 * @author  msu
 */
public class NewWaitingDialog extends javax.swing.JFrame {
    
    /** Creates new form NewWaitingDialog */
    public NewWaitingDialog() {
        myInit();

    }
    
    private void myInit() {
        initComponents();
        
        jProgressBarMain.setMaximum(MAX_PROGRESS_VALUE);
        jProgressBarMain.setMinimum(MIN_PROGRESS_VALUE);
        jProgressBarMain.setValue(MIN_PROGRESS_VALUE);
        jProgressBarMain.setUI( new MetalProgressBarUI() );
        
        //setIconImage( new ImageIcon(getClass().getResource( "/com/cs/gui/img/" + GUIConst.LOGO_FILE_NAME)).getImage() );
//        setIconImage( CustomizedSettings.DIALOG_ICON_IMAGE );
        
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds( (dim.width - WIDTH) / 2, (dim.height - HEIGHT) / 3, WIDTH, HEIGHT);
        
        //initTimer();
        resetStep();
    }

    private void initTimer() {
        /*timer = new Timer( ONE_STEP_TIME, new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                oneStepForward();
            }
        });*/
        timer = new Timer();
        timer.scheduleAtFixedRate( new TimerTask() {
            public void run() { oneStepForward(); }
        }, 0 , ONE_STEP_TIME );
        
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelMessage = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jProgressBarMain = new javax.swing.JProgressBar();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Message");
        setAlwaysOnTop(true);

        jLabelMessage.setFont(new java.awt.Font("DialogInput", 0, 12)); // NOI18N
        jLabelMessage.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelMessage.setText("请稍等。。。");
        getContentPane().add(jLabelMessage, java.awt.BorderLayout.CENTER);

        jPanel1.setPreferredSize(new java.awt.Dimension(100, 40));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jProgressBarMain.setPreferredSize(new java.awt.Dimension(146, 20));
        jPanel1.add(jProgressBarMain, java.awt.BorderLayout.CENTER);

        jPanel2.setPreferredSize(new java.awt.Dimension(20, 100));
        jPanel1.add(jPanel2, java.awt.BorderLayout.EAST);

        jPanel3.setPreferredSize(new java.awt.Dimension(20, 100));
        jPanel1.add(jPanel3, java.awt.BorderLayout.WEST);

        jPanel4.setPreferredSize(new java.awt.Dimension(100, 15));
        jPanel1.add(jPanel4, java.awt.BorderLayout.SOUTH);

        getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * @param args the command line arguments
     */
    /*public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewWaitingDialog().setVisible(true);
            }
        });
    }*/
    
    /* delay is in millisecond
     */
    public void showUp( String message, TimerTask task, int delay ) {
        useProgressValue = false;
        
        setMessage( message );
        setVisible(true);
        
        ( new Timer() ).schedule( task, delay );
        
        //if( timer.isRunning() ) timer.restart();
        //else timer.start();
        initTimer();
    }
    
    /* default start delay is 10ms
     */ 
    public void showUp( String message, TimerTask task ) {
        showUp( message, task, DEFAULT_START_DELAY );
    }
    
    /** similar like showUp, except it let upper caller to set progress by calling setProgressValue() 
     */
    public void showUpUsingProgressValue( String message, TimerTask task, int delay ) {
        resetStep();
        useProgressValue = true;

        setMessage( message );
        setVisible(true);
        
        ( new Timer() ).schedule( task, delay );
        
        //if( timer.isRunning() ) timer.restart();
        //else timer.start();
        initTimer();
   }
    
    /** similar like showUp, except it let upper caller to set progress by calling setProgressValue().
     * default delay is 10ms
     */
    public void showUpUsingProgressValue( String message, TimerTask task ) {
        showUpUsingProgressValue( message, task, DEFAULT_START_DELAY );
    }    
    public void unShow() {
        //if( timer.isRunning() ) timer.stop();
        if ( timer != null ) timer.cancel();
        //timer = null;
        
        setVisible(false);
        resetStep();
    }
  
    
    public void oneStepForward() {
        int value = jProgressBarMain.getValue();
        if ( useProgressValue ) 
            value = progressValue;
        else
            value += STEP;
//System.out.println( "value is " + value + " use progress value " + useProgressValue );    
        if (value > MAX_PROGRESS_VALUE) value = MIN_PROGRESS_VALUE;
        jProgressBarMain.setValue(value);
//System.out.println( "step forward:  " + value  );       
        //repaint();
    }

    public void resetStep() {
        jProgressBarMain.setValue( MIN_PROGRESS_VALUE );
        progressValue = MIN_PROGRESS_VALUE;
        useProgressValue = false;
    }
    
    public void setMessage( String message ) {
        jLabelMessage.setText( message );
    }
    
    /** value is a percentage ranges from 0 ~ 100
     */ 
    public void setProgressValue( int value ) {
        if ( value >= 0 && value <= 100 ) {
            progressValue = ( MAX_PROGRESS_VALUE - MIN_PROGRESS_VALUE ) * value / 100;
        }
    }
    
    private final int WIDTH = 400;
    private final int HEIGHT = 105;
    
    private final int MAX_PROGRESS_VALUE = 100;
    private final int MIN_PROGRESS_VALUE = 0;
    private final int STEP = 2;
    private final int ONE_STEP_TIME = 200;  // in millisecond
    private final int DEFAULT_START_DELAY = 50; // in millisecond
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabelMessage;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JProgressBar jProgressBarMain;
    // End of variables declaration//GEN-END:variables
    
    private Timer timer;
    
    private boolean useProgressValue;
    private int progressValue;
}
